package com.wyg.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wyg.dao.UserDao;

public class PicUpAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	private UserDao udi;
	public UserDao getUdi() {
		return udi;
	}
	public void setUdi(UserDao udi) {
		this.udi = udi;
	}

	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private String title;
	private String allowTypes;
	private String savePath;
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAllowTypes() {
		return allowTypes;
	}
	public void setAllowTypes(String allowTypes) {
		this.allowTypes = allowTypes;
	}
	public String getSavePath() throws Exception{
		return ServletActionContext.getRequest().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return headPic();
	}
	//上传头像
	public String headPic() throws Exception{
		//将允许上传文件类型的字符串以英文逗号,分解成字符串数组，从而判断当前文件类型是否允许上传
			String filterResult = filterType(getAllowTypes().split(","));
			if(filterResult != null){
				addFieldError("upload", "文件类型不正确");
				return filterResult;
			}
			System.out.println(getId());
			System.out.println(udi==null);
			String uphone = udi.getUser(getId()).getPhoneNumber();
			String newName = uphone+uploadFileName.substring(uploadFileName.lastIndexOf("."));
			//以服务器的文件保存地址和文件名建立上传文件输出流
			FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+newName);
			System.out.println(getSavePath());
			System.out.println(newName);
			FileInputStream fis = new FileInputStream(getUpload());
			byte[] buffer = new byte[1024];
			int len = 0;
			while((len = fis.read(buffer))>0){
				fos.write(buffer,0,len);
			}
			setUploadFileName(newName);
			fos.close();
			fis.close();
			String path = "../userpic/"+newName;
			udi.setPicPath(id, path);
			return "success";
	}
	
	/*
	 * 过滤文件类型
	 * 
	 */
	public String filterType(String[] types){
		//获取希望上传的文件类型
		String fileType = getUploadContentType();
		for(String type:types){
			if(type.equals(fileType)){
				return null;
			}
		}
		return "input";
	}
	
}
